{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: langchain==0.2.7 in .\\.venv\\lib\\site-packages (from -r requirements.txt (line 1)) (0.2.7)\n",
      "Requirement already satisfied: langchain-community==0.2.7 in .\\.venv\\lib\\site-packages (from -r requirements.txt (line 2)) (0.2.7)\n",
      "Requirement already satisfied: langchain-experimental==0.0.62 in .\\.venv\\lib\\site-packages (from -r requirements.txt (line 3)) (0.0.62)\n",
      "Requirement already satisfied: langchain_openai==0.1.14 in .\\.venv\\lib\\site-packages (from -r requirements.txt (line 4)) (0.1.14)\n",
      "Requirement already satisfied: PyYAML>=5.3 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (6.0.1)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (2.0.31)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (3.9.5)\n",
      "Requirement already satisfied: langchain-core<0.3.0,>=0.2.12 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (0.2.12)\n",
      "Requirement already satisfied: langchain-text-splitters<0.3.0,>=0.2.0 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (0.2.2)\n",
      "Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (0.1.84)\n",
      "Requirement already satisfied: numpy<2,>=1 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (1.26.4)\n",
      "Requirement already satisfied: pydantic<3,>=1 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (2.8.2)\n",
      "Requirement already satisfied: requests<3,>=2 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (2.32.3)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in .\\.venv\\lib\\site-packages (from langchain==0.2.7->-r requirements.txt (line 1)) (8.5.0)\n",
      "Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in .\\.venv\\lib\\site-packages (from langchain-community==0.2.7->-r requirements.txt (line 2)) (0.6.7)\n",
      "Requirement already satisfied: openai<2.0.0,>=1.32.0 in .\\.venv\\lib\\site-packages (from langchain_openai==0.1.14->-r requirements.txt (line 4)) (1.35.10)\n",
      "Requirement already satisfied: tiktoken<1,>=0.7 in .\\.venv\\lib\\site-packages (from langchain_openai==0.1.14->-r requirements.txt (line 4)) (0.7.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain==0.2.7->-r requirements.txt (line 1)) (1.3.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain==0.2.7->-r requirements.txt (line 1)) (23.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain==0.2.7->-r requirements.txt (line 1)) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain==0.2.7->-r requirements.txt (line 1)) (6.0.5)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain==0.2.7->-r requirements.txt (line 1)) (1.9.4)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in .\\.venv\\lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain-community==0.2.7->-r requirements.txt (line 2)) (3.21.3)\n",
      "Requirement already satisfied: typing-inspect<1,>=0.4.0 in .\\.venv\\lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain-community==0.2.7->-r requirements.txt (line 2)) (0.9.0)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in .\\.venv\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain==0.2.7->-r requirements.txt (line 1)) (1.33)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in .\\.venv\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain==0.2.7->-r requirements.txt (line 1)) (24.1)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in .\\.venv\\lib\\site-packages (from langsmith<0.2.0,>=0.1.17->langchain==0.2.7->-r requirements.txt (line 1)) (3.10.6)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (4.4.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (0.27.0)\n",
      "Requirement already satisfied: sniffio in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (1.3.1)\n",
      "Requirement already satisfied: tqdm>4 in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (4.66.4)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.7 in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (4.12.2)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in .\\.venv\\lib\\site-packages (from pydantic<3,>=1->langchain==0.2.7->-r requirements.txt (line 1)) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.20.1 in .\\.venv\\lib\\site-packages (from pydantic<3,>=1->langchain==0.2.7->-r requirements.txt (line 1)) (2.20.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in .\\.venv\\lib\\site-packages (from requests<3,>=2->langchain==0.2.7->-r requirements.txt (line 1)) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in .\\.venv\\lib\\site-packages (from requests<3,>=2->langchain==0.2.7->-r requirements.txt (line 1)) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in .\\.venv\\lib\\site-packages (from requests<3,>=2->langchain==0.2.7->-r requirements.txt (line 1)) (2.2.2)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in .\\.venv\\lib\\site-packages (from requests<3,>=2->langchain==0.2.7->-r requirements.txt (line 1)) (2024.7.4)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in .\\.venv\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain==0.2.7->-r requirements.txt (line 1)) (3.0.3)\n",
      "Requirement already satisfied: regex>=2022.1.18 in .\\.venv\\lib\\site-packages (from tiktoken<1,>=0.7->langchain_openai==0.1.14->-r requirements.txt (line 4)) (2024.5.15)\n",
      "Requirement already satisfied: httpcore==1.* in .\\.venv\\lib\\site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (1.0.5)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in .\\.venv\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (0.14.0)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in .\\.venv\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.12->langchain==0.2.7->-r requirements.txt (line 1)) (3.0.0)\n",
      "Requirement already satisfied: colorama in .\\.venv\\lib\\site-packages (from tqdm>4->openai<2.0.0,>=1.32.0->langchain_openai==0.1.14->-r requirements.txt (line 4)) (0.4.6)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in .\\.venv\\lib\\site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain-community==0.2.7->-r requirements.txt (line 2)) (1.0.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 23.1.2 -> 24.1.2\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "%pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- ChatOpenAI是LangChain“Runnables”的实例，这意味着它们公开了一个与它们交互的标准接口。要简单地调用模型，我们可以将消息列表传递给.invoke方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(model=\"gpt-4o\", verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='你好！', response_metadata={'token_usage': {'completion_tokens': 2, 'prompt_tokens': 20, 'total_tokens': 22}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_abc28019ad', 'finish_reason': 'stop', 'logprobs': None}, id='run-5ae1a033-3240-4b51-9213-8a653b557361-0', usage_metadata={'input_tokens': 20, 'output_tokens': 2, 'total_tokens': 22})"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import HumanMessage, SystemMessage\n",
    "\n",
    "messages = [\n",
    "    SystemMessage(content=\"Translate the following from English into Chinese\"),\n",
    "    HumanMessage(content=\"hi!\"),\n",
    "]\n",
    "\n",
    "llm.invoke(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- StrOutputParser 输出解析器，也是LangChain“Runnables”的实例，同样也可以将内容传递给.invoke方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "\n",
    "parser = StrOutputParser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'嗨！'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = llm.invoke(messages)\n",
    "parser.invoke(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 我们可以使用|运算符轻松创建链。|运算符在LangChain中用于将两个元素组合在一起。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'嗨!'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm_chain = llm | parser\n",
    "\n",
    "llm_chain.invoke(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- PromptTemplate是LangChain中的一个概念，旨在帮助实现这种转换。它们接受原始用户输入并返回准备传递到语言模型的数据（提示）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "system_template = \"Translate the following into {language}:\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 接下来，创建PromptTemplate提示对象。这将是system_template的组合，以及一个更简单的模板，用于放置要翻译的文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template = ChatPromptTemplate.from_messages(\n",
    "    [(\"system\", system_template), (\"user\", \"{text}\")]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 同样的，PromptTemplate也是LangChain“Runnables”的实例，同样也可以将内容传递给.invoke方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = prompt_template.invoke(\n",
    "    {\n",
    "        \"language\": \"chinese\", \n",
    "        \"text\": \"hello world\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SystemMessage(content='Translate the following into chinese:'),\n",
       " HumanMessage(content='hello world')]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.to_messages()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 现在可以使用管道（|）运算符将其与上面的模型和输出解析器结合起来："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'LangChain 是一个用于开发由大型语言模型（LLMs）驱动的应用程序的框架。'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm_chain = prompt_template | llm | parser\n",
    "\n",
    "llm_chain.invoke(\n",
    "    {\n",
    "        \"language\": \"chinese\",\n",
    "        \"text\": \"LangChain is a framework for developing applications powered by large language models (LLMs).\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 现在我们已经构建了一个应用程序，我们需要为它提供服务。这就是LangServe的用武之地。\n",
    "- LangServe帮助开发人员将LangChain链部署为REST API。您无需使用LangServe即可使用LangChain，\n",
    "- 但在本指南中，我们将展示如何使用LangServe部署您的应用程序。\n",
    "- 安装依赖 pip install \"langserve[all]\"\n",
    "- 启动服务 python translation-app.py\n",
    "- 内置的测试页面：http://localhost:8000/chain/playground/\n",
    "- http://localhost:8000/chain/\n",
    "\n",
    "\n",
    "- 在代码中使用客户端进行访问服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from langserve import RemoteRunnable\n",
    "\n",
    "# remote_chain = RemoteRunnable(\"http://localhost:8000/chain/\")\n",
    "# remote_chain.invoke({\"language\": \"chinese\", \"text\": \"hi\"})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
